package in.muton.edushare.auth.roles;

import java.util.List;

import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UnivRoleCheck {

	public UnivRoleCheck(Universities univ, Users user) {
		this.u = user;
		this.univ = univ;
	}

	public boolean isMember() {
		return getRole(univ, u) != 0;
	}
	
	public boolean isInvited() {
		return isInvited(univ, u);
	}
	
	public boolean isAdmin() {
		return isAdmin(univ, u);
	}

	public boolean isStudent() {
		return isStudent(univ, u);
	}

	public boolean isTeacher() {
		return isTeacher(univ, u);
	}

	public static int getRole(Universities univ, Users user) {
		List<UserRoles> uRList = user.getRoleList();
		for (UserRoles u : uRList) {
			if (u.getUniversity() == univ
					&& u.getStatus() == UserRoles.STATUS_ACTIVE)
				return u.getRoleType();
		}
		return 0;
	}

	public static boolean isAdmin(Universities univ, Users user) {
		return (getRole(univ, user) == UserRoles.ADMIN_ROLE);
	}

	public static boolean isTeacher(Universities univ, Users user) {
		return (getRole(univ, user) == UserRoles.TEACHER_ROLE);
	}

	public static boolean isStudent(Universities univ, Users user) {
		return (getRole(univ, user) == UserRoles.STUDENT_ROLE);
	}
	
	public static boolean isInvited(Universities univ, Users user) {
		List<UserRoles> uRList = user.getRoleList();
		for (UserRoles u : uRList) {
			if (u.getStatus() == UserRoles.STATUS_INVITED)
				return true;
		}
		return false;
	}
	
	public String getRoleAsString(){
		return UserRoles.getRoleAsString(getRole(this.univ, this.u));
	}
	
	public int getRole(){
		return getRole(univ, u);
	}
	
	private Universities univ;
	private Users u;
}
